<template>
  <div class="user-info">
    <span class="user-nickname">{{ userLoginData.user_name }}</span
    ><br />
    <div class="dropdown">
      <img src="../../../../assets/icon/arrowDown.svg" alt="" />
      <div class="dropdown-content">
        <span @click="handelOutUsername(userId, userRole)">退出登录</span><br />
        <span @click="handlePutPassword(userName)">修改密码</span><br />
        <span v-if="isAdmin" @click="handleManageuser">管理用户</span>
      </div>
    </div>
    <UserManage v-if="isManageUser" />
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import UserManage from './components/UserManage/UserManage.vue'
import { userModules } from '../../../../stores/userModulsStore'

const router = useRouter()
const isAdmin = ref(false)
const userName = ref('')
const userId = ref()
const userRole = ref()
const isManageUser = ref(false)
const emit = defineEmits(['out'])

const { userLoginData } = userModules()
// 加载初始数据
onMounted(async () => {
  const { user_name, user_role, id } = userLoginData.value
  userId.value = id
  userRole.value = user_role
  userName.value = user_name
  if (user_role === 2) {
    isAdmin.value = true
  }
})

// 修改密码
const handlePutPassword = (name) => {
  router.push({
    path: 'login',
    query: { username: name }
  })
}

// 退出登录
const handelOutUsername = (userId) => {
  emit('out', userId)
  location.reload()
}

// 管理用户
const handleManageuser = () => {
  isManageUser.value = !isManageUser.value
  // console.log(isManageUser.value)
}
</script>

<style lang="less" scoped>
@import './styles/islogin.css';
</style>
